package org.kjtc.balanceController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.balanceEntity.MeterManage;
import org.kjtc.balanceService.MeterManageService;
import org.kjtc.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MeterManageController extends BaseController{

	@Resource(name = "meterManageService")
	private MeterManageService meterManageService;

	@RequestMapping(value = "/searchMeterManage")
	@ResponseBody
	public Map<String, Object> searchMeterManage(MeterManage meterManage){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info("电表管理查询开始");

		// 获取所有设备数据
		List<MeterManage> list = null;
		try {
			list = meterManageService.getMeterManage(meterManage);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("电表管理查询异常" + e.getMessage());
			return map;
		}
		map.put("meterManageList", list);

		logger.info("电表管理查询结束");
		meterManage = null;
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/saveMeterManage")
	public Map<String, Object> saveMeterManage(MeterManage meterManage){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info("电表管理添加开始");
		meterManage.setCreateUser(this.getUesr().getUserID());
		meterManage.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = meterManageService.insertMeterManage(meterManage);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		}  catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error("电表管理添加异常");
		}
		logger.info("电表管理添加结束");
		meterManage = null;
		return map;
	}

	@RequestMapping(value = "/updateMeterManage")
	@ResponseBody
	public Map<String, Object> updateMeterManage(MeterManage meterManage) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info("电表管理修改开始");
		meterManage.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = meterManageService.updateMeterManage(meterManage);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error("电表管理修改异常");
		}
		logger.info("电表管理修改结束");
		meterManage = null;
		return map;
	}

	@RequestMapping(value = "/deleteMeterManage")
	@ResponseBody
	public Object deleteMeterManage(MeterManage meterManage) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info("电表管理删除开始");
		meterManage.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = meterManageService.deleteMeterManage(meterManage);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error("电表管理删除异常:" + e.getMessage());
		}
		logger.info("电表管理删除结束");
		meterManage = null;
		return map;
	}
}
